Ethereum by Bruno Skvorc
Author:Bruno Skvorc [Various]
Language: eng
Format: epub
Publisher: SitePoint
Published: 2017-08-08T23:00:00+00:00
JavaScript Testing
Truffle enables us to use JavaScript for testing, leveraging the Mocha testing framework. This enables you to write more complex tests and get more functionality out of your testing framework.
Okay, let’s write the test. First, in the test folder, create a file and call it hashmarket.js.
The first thing we need to do, is get the reference to our contract in JavaScript. For that, we’ll use Truffle’s artifacts.require(...) function:
var HashMarket = artifacts.require("./HashMarket.sol");
Now that we have the reference to the contract, let’s start writing tests. To start, we’ll use the contract function provided to us:
contract("HashMarket", function(accounts) { });
This creates a test suite for our contract. Now for testing, we use Mocha it syntax:
contract("HashMarket", function(accounts) { it("should add a new product", function() { }); });
This describes the test we’ll write and presents a message for us to know the purpose of the test. Now let’s write the test itself. At the end, the hashmarket.js file should look like the following. The reasoning is explained in the comments of the source code:
var HashMarket = artifacts.require("./HashMarket.sol"); contract("HashMarket", function(accounts) { it("should add a new product", function() { // Set the names of test data var itemName = "TestItem"; var itemPrice = 1000; var itemSeller = accounts[0]; // Since all of our testing functions are async, we store the // contract instance at a higher level to enable access from // all functions var hashMarketContract; // Item ID will be provided asynchronously so we extract it var itemID; return HashMarket.deployed().then(function(instance) { // set contract instance into a variable hashMarketContract = instance; // Subscribe to a Solidity event instance.ItemAdded({}).watch((error, result) => { if (error) { console.log(error); } // Once the event is triggered, store the result in the // external variable itemID = result.args.itemID; }); // Call the addNewItem function and return the promise return instance.addNewItem(itemName, itemPrice, {from: itemSeller}); }).then(function() { // This function is triggered after the addNewItem call transaction // has been mined. Now call the getItem function with the itemID // we received from the event return hashMarketContract.getItem.call(itemID); }).then(function(result) { // The result of getItem is a tuple, we can deconstruct it // to variables like this var [name, price, seller, status] = result; // Start testing. Use web3.toAscii() to convert the result of // the smart contract from Solidity bytecode to ASCII. After that // use the .replace() to pad the excess bytes from bytes32 assert.equal(itemName, web3.toAscii(name).replace(/\u0000/g, ''), "Name wasn't properly added"); // Use assert.equal() to check all the variables assert.equal(itemPrice, price, "Price wasn't properly added"); assert.equal(itemSeller, seller, "Seller wasn't properly added"); assert.equal(status, 0, "Status wasn't properly added"); }); }); });
Run truffle test and you should get something like this:
TestHashMarket ✓ testAddingNewProduct (109ms) Contract: HashMarket ✓ should add a new product (64ms) 2 passing (876ms)
Your test has passed and you can be confident about not having regression bugs.
For homework, write tests for all other functions in the contract.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27095)
Hello! Python by Anthony Briggs(25950)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25286)
Kotlin in Action by Dmitry Jemerov(24396)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23591)
Dependency Injection in .NET by Mark Seemann(23313)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21945)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20849)
Grails in Action by Glen Smith Peter Ledbrook(19869)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17073)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16833)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14464)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12584)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11865)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10650)
Hit Refresh by Satya Nadella(9239)
The Kubernetes Operator Framework Book by Michael Dame(8588)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8446)
Robo-Advisor with Python by Aki Ranin(8390)